package org.zerg.controller.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.zerg.dto.SessionKeyConst;
import org.zerg.entity.Manage;
import org.zerg.enums.CodeEnum;
import org.zerg.service.ManageService;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private ManageService manageService;
	
	@Autowired
	private HttpSession session;
	
	@RequestMapping
	public String index() {
		return "/main";
	}
	
	//session超时
	@RequestMapping("/sessionTimeout")
	public String sessionTimeout() {
		session.setAttribute(CodeEnum.KEY, CodeEnum.LOGIN_UNKNOWN);
		return "/login";
	}
	
	@RequestMapping("/validate")
	public String validate(@RequestParam("admin")String admin,@RequestParam("password")String password,RedirectAttributes attr) {
		Manage manage = manageService.checkManage(admin, password);
		if(manage!=null){
			session.setAttribute(SessionKeyConst.MANAGE_NAME, manage.getName());
			session.setAttribute(SessionKeyConst.MANAGE_ROLE, manage.getRole());
			return "redirect:/login";
		}
		session.setAttribute(CodeEnum.KEY, CodeEnum.LOGIN_FALSE);
		return "/login";
	}
	
	@RequestMapping("/ref")
	public void ref() {
		session.setAttribute(CodeEnum.KEY, CodeEnum.LOGIN_UNKNOWN);
	}
	
}
